package cuit.campus.taskRunning.user.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cuit.campus.taskRunning.user.entry.po.User;
import cuit.campus.taskRunning.user.mapper.UserMapper;
import cuit.campus.taskRunning.commons.error.UserDeletedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserDetailServiceimpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        var wrapper = new LambdaQueryWrapper<User>().eq(User::getUsername, username).eq(User::getIsDelete,0);
        User user = userMapper.selectOne(wrapper);
        log.debug("Found user: {}", user);
        if(user == null)
            throw new UsernameNotFoundException("用户不存在");
        return user;
    }
}
